package com.API.dal.providers;

import com.API.dal.entities.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class OrdersProviderImp implements OrdersProvider {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addOrder(Orders order) {
        Session session = sessionFactory.getCurrentSession();
        session.save(order);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addEquipmentToOrder(int price, int quantity, Equipments equipment, Orders order) {
        Session session = sessionFactory.getCurrentSession();
        OrdersEquipmentsId ordersEquipmentsId = new OrdersEquipmentsId();
        ordersEquipmentsId.setOrdersId(order.getId());
        ordersEquipmentsId.setEquipmentId(equipment.getId());
        ordersEquipmentsId.setPrice(BigDecimal.valueOf(price));
        ordersEquipmentsId.setQuantity(quantity);
        OrdersEquipments ordersEquipments = new OrdersEquipments(ordersEquipmentsId,
                order, equipment);
        session.save(ordersEquipments);

    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateEquipmentInOrder(Equipments equipment, Orders order, int quantity, int price) {
        Session session = sessionFactory.getCurrentSession();
        OrdersEquipmentsId ordersEquipmentsId = new OrdersEquipmentsId();
        ordersEquipmentsId.setOrdersId(order.getId());
        ordersEquipmentsId.setEquipmentId(equipment.getId());
        ordersEquipmentsId.setPrice(BigDecimal.valueOf(price));
        ordersEquipmentsId.setQuantity(quantity);
        OrdersEquipments ordersEquipments = new OrdersEquipments(ordersEquipmentsId,
                order, equipment);
        session.update(ordersEquipments);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateOrder(Orders order) {
        Session session = sessionFactory.getCurrentSession();
        session.update(order);
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getOrdersByPayType(PaymentsTypes paymentsTypes) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord " +
                "where ord.paymentsTypes.id = " + paymentsTypes.getId());
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getOrdersByPayType(int paymentsTypes_id) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord " +
                "where ord.paymentsTypes.id = " + paymentsTypes_id);
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getOrdersByPayType(String paymentsTypes_name) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord,PaymentsTypes pt " +
                "where ord.paymentsTypes.id = pt.id and pt.name = " + paymentsTypes_name);
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getOrders() {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord");
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getEquipmentOrders(int equipment_id) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord, OrdersEquipments oe " +
                "where ord.id = oe.orders.id and oe.equipments.id = " + equipment_id);
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getEquipmentOrders(String equipment_name) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord, OrdersEquipments oe " +
                "where ord.id = oe.orders.id and oe.equipments.name = " + equipment_name);
        ordersList = query.list();
        return ordersList;
    }


    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getOrdersBySupplier(int supplier_id) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord" +
                " where ord.objectsByProviderId.id = " + supplier_id);
        ordersList = query.list();
        return ordersList;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Orders> getOrdersBySupplier(String supplier_name) {
        List<Orders> ordersList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Orders ord" +
                " where ord.objectsByProviderId.name = " + supplier_name);
        ordersList = query.list();
        return ordersList;
    }
}
